package 领扣算法.力扣计划.算法基础.子集II;

import java.util.*;

public class Main {
    LinkedList<List<Integer>> res=new LinkedList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        res.add(new LinkedList<>());
        Arrays.sort(nums);
        LinkedList<Integer> data=new LinkedList<>();
        for (int num : nums) {
            data.add(num);
        }
        int middle=Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            int middle2=data.pollFirst();
            if (middle2==middle)
                continue;
            else {
                middle=middle2;
                LinkedList<Integer> integers = new LinkedList<>();
                integers.add(middle);
                plus(new LinkedList<Integer>(data),integers);
            }
        }
        return res;
    }

    private void plus(LinkedList<Integer> integers, LinkedList<Integer> re) {
        res.add(re);
        if (integers.isEmpty()){
            return;
        }
        int middle=Integer.MAX_VALUE;
        while (!integers.isEmpty()){
            int middle1=integers.pollFirst();
            if (middle==middle1)
                continue;
            middle=middle1;
            LinkedList<Integer> list=new LinkedList<>(integers);
            LinkedList<Integer> list_re=new LinkedList<>(re);
            list_re.add(middle);
            plus(list,list_re);
        }
    }
}
